home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / WarpQuake / Src / sys_linux.c < prev    next >
C/C++ Source or Header  |  2000-05-22  |  13KB  |  458 lines

  1. #include <unistd.h>
  2. #include <signal.h>
  3. #include <stdlib.h>
  4. #include <limits.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include <sys/ipc.h>
  12. #include <sys/shm.h>
  13. #include <sys/stat.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <sys/wait.h>
  17. #include <sys/mman.h>
  18. #include <errno.h>
  19.  
  20. #include "quakedef.h"
  21.  
  22. qboolean            isDedicated;
  23.  
  24. int nostdout = 0;
  25.  
  26. char *basedir = ".";
  27. char *cachedir = "/tmp";
  28.  
  29. cvar_t  sys_linerefresh = {"sys_linerefresh","0"};// set for entity display
  30.  
  31. // =======================================================================
  32. // General routines
  33. // =======================================================================
  34.  
  35. void Sys_DebugNumber(int y, int val)
  36. {
  37. }
  38.  
  39. /*
  40. void Sys_Printf (char *fmt, ...)
  41. {
  42.     va_list        argptr;
  43.     char        text[1024];
  44.     
  45.     va_start (argptr,fmt);
  46.     vsprintf (text,fmt,argptr);
  47.     va_end (argptr);
  48.     fprintf(stderr, "%s", text);
  49.     
  50.     Con_Print (text);
  51. }
  52.  
  53. void Sys_Printf (char *fmt, ...)
  54. {
  55.  
  56.     va_list     argptr;
  57.     char        text[1024], *t_p;
  58.     int         l, r;
  59.  
  60.     if (nostdout)
  61.         return;
  62.  
  63.     va_start (argptr,fmt);
  64.     vsprintf (text,fmt,argptr);
  65.     va_end (argptr);
  66.  
  67.     l = strlen(text);
  68.     t_p = text;
  69.  
  70. // make sure everything goes through, even though we are non-blocking
  71.     while (l)
  72.     {
  73.         r = write (1, text, l);
  74.         if (r != l)
  75.             sleep (0);
  76.         if (r > 0)
  77.         {
  78.             t_p += r;
  79.             l -= r;
  80.         }
  81.     }
  82.  
  83. }
  84. */
  85.  
  86. void Sys_Printf (char *fmt, ...)
  87. {
  88.     va_list        argptr;
  89.     char        text[1024];
  90.     unsigned char        *p;
  91.  
  92.     va_start (argptr,fmt);
  93.     vsprintf (text,fmt,argptr);
  94.     va_end (argptr);
  95.  
  96.     if (strlen(text) > sizeof(text))
  97.         Sys_Error("memory overwrite in Sys_Printf");
  98.  
  99.     if (nostdout)
  100.         return;
  101.  
  102.     for (p = (unsigned char *)text; *p; p++) {
  103.         *p &= 0x7f;
  104.         if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
  105.             printf("[%02x]", *p);
  106.         else
  107.             putc(*p, stdout);
  108.     }
  109. }
  110.  
  111. #if 0
  112. static char end1[] =
  113. "\x1b[?7h\x1b[40m\x1b[2J\x1b[0;1;41m\x1b[1;1H                QUAKE: The Doomed Dimension \x1b[33mby \x1b[44mid\x1b[41m Software                      \x1b[2;1H  ----------------------------------------------------------------------------  \x1b[3;1H           CALL 1-800-IDGAMES TO ORDER OR FOR TECHNICAL SUPPORT                 \x1b[4;1H             PRICE: $45.00 (PRICES MAY VARY OUTSIDE THE US.)                    \x1b[5;1H                                                                                \x1b[6;1H  \x1b[37mYes! You only have one fourth of this incredible epic. That is because most   \x1b[7;1H   of you have paid us nothing or at most, very little. You could steal the     \x1b[8;1H   game from a friend. But we both know you'll be punished by God if you do.    \x1b[9;1H        \x1b[33mWHY RISK ETERNAL DAMNATION? CALL 1-800-IDGAMES AND BUY NOW!             \x1b[10;1H             \x1b[37mRemember, we love you almost as much as He does.                   \x1b[11;1H                                                                                \x1b[12;1H            \x1b[33mProgramming: \x1b[37mJohn Carmack, Michael Abrash, John Cash                \x1b[13;1H       \x1b[33mDesign: \x1b[37mJohn Romero, Sandy Petersen, American McGee, Tim Willits         \x1b[14;1H                     \x1b[33mArt: \x1b[37mAdrian Carmack, Kevin Cloud                           \x1b[15;1H               \x1b[33mBiz: \x1b[37mJay Wilbur, Mike Wilson, Donna Jackson                      \x1b[16;1H            \x1b[33mProjects: \x1b[37mShawn Green   \x1b[33mSupport: \x1b[37mBarrett Alexander                  \x1b[17;1H              \x1b[33mSound Effects: \x1b[37mTrent Reznor and Nine Inch Nails                   \x1b[18;1H  For other information or details on ordering outside the US, check out the    \x1b[19;1H     files accompanying QUAKE or our website at http://www.idsoftware.com.      \x1b[20;1H    \x1b[0;41mQuake is a trademark of Id Software, inc., (c)1996 Id Software, inc.        \x1b[21;1H     All rights reserved. NIN logo is a registered trademark licensed to        \x1b[22;1H                 Nothing Interactive, Inc. All rights reserved.                 \x1b[40m\x1b[23;1H\x1b[0m";
  114. static char end2[] =
  115. "\x1b[?7h\x1b[40m\x1b[2J\x1b[0;1;41m\x1b[1;1H        QUAKE \x1b[33mby \x1b[44mid\x1b[41m Software                                                    \x1b[2;1H -----------------------------------------------------------------------------  \x1b[3;1H        \x1b[37mWhy did you quit from the registered version of QUAKE? Did the          \x1b[4;1H        scary monsters frighten you? Or did Mr. Sandman tug at your             \x1b[5;1H        little lids? No matter! What is important is you love our               \x1b[6;1H        game, and gave us your money. Congratulations, you are probably         \x1b[7;1H        not a thief.                                                            \x1b[8;1H                                                           Thank You.           \x1b[9;1H        \x1b[33;44mid\x1b[41m Software is:                                                         \x1b[10;1H        PROGRAMMING: \x1b[37mJohn Carmack, Michael Abrash, John Cash                    \x1b[11;1H        \x1b[33mDESIGN: \x1b[37mJohn Romero, Sandy Petersen, American McGee, Tim Willits        \x1b[12;1H        \x1b[33mART: \x1b[37mAdrian Carmack, Kevin Cloud                                        \x1b[13;1H        \x1b[33mBIZ: \x1b[37mJay Wilbur, Mike Wilson     \x1b[33mPROJECTS MAN: \x1b[37mShawn Green              \x1b[14;1H        \x1b[33mBIZ ASSIST: \x1b[37mDonna Jackson        \x1b[33mSUPPORT: \x1b[37mBarrett Alexander             \x1b[15;1H        \x1b[33mSOUND EFFECTS AND MUSIC: \x1b[37mTrent Reznor and Nine Inch Nails               \x1b[16;1H                                                                                \x1b[17;1H        If you need help running QUAKE refer to the text files in the           \x1b[18;1H        QUAKE directory, or our website at http://www.idsoftware.com.           \x1b[19;1H        If all else fails, call our technical support at 1-800-IDGAMES.         \x1b[20;1H      \x1b[0;41mQuake is a trademark of Id Software, inc., (c)1996 Id Software, inc.      \x1b[21;1H        All rights reserved. NIN logo is a registered trademark licensed        \x1b[22;1H             to Nothing Interactive, Inc. All rights reserved.                  \x1b[23;1H\x1b[40m\x1b[0m";
  116.  
  117. #endif
  118. void Sys_Quit (void)
  119. {
  120.     Host_Shutdown();
  121.     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  122. #if 0
  123.     if (registered.value)
  124.         printf("%s", end2);
  125.     else
  126.         printf("%s", end1);
  127. #endif
  128.     fflush(stdout);
  129.     exit(0);
  130. }
  131.  
  132. void Sys_Init(void)
  133. {
  134. #if id386
  135.     Sys_SetFPCW();
  136. #endif
  137. }
  138.  
  139. void Sys_Error (char *error, ...)
  140.     va_list     argptr;
  141.     char        string[1024];
  142.  
  143. // change stdin to non blocking
  144.     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  145.     
  146.     va_start (argptr,error);
  147.     vsprintf (string,error,argptr);
  148.     va_end (argptr);
  149.     fprintf(stderr, "Error: %s\n", string);
  150.  
  151.     Host_Shutdown ();
  152.     exit (1);
  153.  
  154.  
  155. void Sys_Warn (char *warning, ...)
  156.     va_list     argptr;
  157.     char        string[1024];
  158.     
  159.     va_start (argptr,warning);
  160.     vsprintf (string,warning,argptr);
  161.     va_end (argptr);
  162.     fprintf(stderr, "Warning: %s", string);
  163.  
  164. /*
  165. ============
  166. Sys_FileTime
  167.  
  168. returns -1 if not present
  169. ============
  170. */
  171. int    Sys_FileTime (char *path)
  172. {
  173.     struct    stat    buf;
  174.     
  175.     if (stat (path,&buf) == -1)
  176.         return -1;
  177.     
  178.     return buf.st_mtime;
  179. }
  180.  
  181.  
  182. void Sys_mkdir (char *path)
  183. {
  184.     mkdir (path, 0777);
  185. }
  186.  
  187. int Sys_FileOpenRead (char *path, int *handle)
  188. {
  189.     int    h;
  190.     struct stat    fileinfo;
  191.     
  192.     
  193.     h = open (path, O_RDONLY, 0666);
  194.     *handle = h;
  195.     if (h == -1)
  196.         return -1;
  197.     
  198.     if (fstat (h,&fileinfo) == -1)
  199.         Sys_Error ("Error fstating %s", path);
  200.  
  201.     return fileinfo.st_size;
  202. }
  203.  
  204. int Sys_FileOpenWrite (char *path)
  205. {
  206.     int     handle;
  207.  
  208.     umask (0);
  209.     
  210.     handle = open(path,O_RDWR | O_CREAT | O_TRUNC
  211.     , 0666);
  212.  
  213.     if (handle == -1)
  214.         Sys_Error ("Error opening %s: %s", path,strerror(errno));
  215.  
  216.     return handle;
  217. }
  218.  
  219. int Sys_FileWrite (int handle, void *src, int count)
  220. {
  221.     return write (handle, src, count);
  222. }
  223.  
  224. void Sys_FileClose (int handle)
  225. {
  226.     close (handle);
  227. }
  228.  
  229. void Sys_FileSeek (int handle, int position)
  230. {
  231.     lseek (handle, position, SEEK_SET);
  232. }
  233.  
  234. int Sys_FileRead (int handle, void *dest, int count)
  235. {
  236.     return read (handle, dest, count);
  237. }
  238.  
  239. void Sys_DebugLog(char *file, char *fmt, ...)
  240. {
  241.     va_list argptr; 
  242.     static char data[1024];
  243.     int fd;
  244.     
  245.     va_start(argptr, fmt);
  246.     vsprintf(data, fmt, argptr);
  247.     va_end(argptr);
  248. //    fd = open(file, O_WRONLY | O_BINARY | O_CREAT | O_APPEND, 0666);
  249.     fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
  250.     write(fd, data, strlen(data));
  251.     close(fd);
  252. }
  253.  
  254. void Sys_EditFile(char *filename)
  255. {
  256.  
  257.     char cmd[256];
  258.     char *term;
  259.     char *editor;
  260.  
  261.     term = getenv("TERM");
  262.     if (term && !strcmp(term, "xterm"))
  263.     {
  264.         editor = getenv("VISUAL");
  265.         if (!editor)
  266.             editor = getenv("EDITOR");
  267.         if (!editor)
  268.             editor = getenv("EDIT");
  269.         if (!editor)
  270.             editor = "vi";
  271.         sprintf(cmd, "xterm -e %s %s", editor, filename);
  272.         system(cmd);
  273.     }
  274.  
  275. }
  276.  
  277. double Sys_FloatTime (void)
  278. {
  279.     struct timeval tp;
  280.     struct timezone tzp; 
  281.     static int      secbase; 
  282.     
  283.     gettimeofday(&tp, &tzp);  
  284.  
  285.     if (!secbase)
  286.     {
  287.         secbase = tp.tv_sec;
  288.         return tp.tv_usec/1000000.0;
  289.     }
  290.  
  291.     return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
  292. }
  293.  
  294. // =======================================================================
  295. // Sleeps for microseconds
  296. // =======================================================================
  297.  
  298. static volatile int oktogo;
  299.  
  300. void alarm_handler(int x)
  301. {
  302.     oktogo=1;
  303. }
  304.  
  305. void Sys_LineRefresh(void)
  306. {
  307. }
  308.  
  309. void floating_point_exception_handler(int whatever)
  310. {
  311. //    Sys_Warn("floating point exception\n");
  312.     signal(SIGFPE, floating_point_exception_handler);
  313. }
  314.  
  315. char *Sys_ConsoleInput(void)
  316. {
  317.     static char text[256];
  318.     int     len;
  319.     fd_set    fdset;
  320.     struct timeval timeout;
  321.  
  322.     if (cls.state == ca_dedicated) {
  323.         FD_ZERO(&fdset);
  324.         FD_SET(0, &fdset); // stdin
  325.         timeout.tv_sec = 0;
  326.         timeout.tv_usec = 0;
  327.         if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
  328.             return NULL;
  329.  
  330.         len = read (0, text, sizeof(text));
  331.         if (len < 1)
  332.             return NULL;
  333.         text[len-1] = 0;    // rip off the /n and terminate
  334.  
  335.         return text;
  336.     }
  337.     return NULL;
  338. }
  339.  
  340. #if !id386
  341. void Sys_HighFPPrecision (void)
  342. {
  343. }
  344.  
  345. void Sys_LowFPPrecision (void)
  346. {
  347. }
  348. #endif
  349.  
  350. int main (int c, char **v)
  351. {
  352.  
  353.     double        time, oldtime, newtime;
  354.     quakeparms_t parms;
  355.     extern int vcrFile;
  356.     extern int recording;
  357.     int j;
  358.  
  359. //    static char cwd[1024];
  360.  
  361. //    signal(SIGFPE, floating_point_exception_handler);
  362.     signal(SIGFPE, SIG_IGN);
  363.  
  364.     memset(&parms, 0, sizeof(parms));
  365.  
  366.     COM_InitArgv(c, v);
  367.     parms.argc = com_argc;
  368.     parms.argv = com_argv;
  369.  
  370. #ifdef GLQUAKE
  371.     parms.memsize = 16*1024*1024;
  372. #else
  373.     parms.memsize = 8*1024*1024;
  374. #endif
  375.  
  376.     j = COM_CheckParm("-mem");
  377.     if (j)
  378.         parms.memsize = (int) (Q_atof(com_argv[j+1]) * 1024 * 1024);
  379.     parms.membase = malloc (parms.memsize);
  380.  
  381.     parms.basedir = basedir;
  382. // caching is disabled by default, use -cachedir to enable
  383. //    parms.cachedir = cachedir;
  384.  
  385.     fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
  386.  
  387.     Host_Init(&parms);
  388.  
  389.     Sys_Init();
  390.  
  391.     if (COM_CheckParm("-nostdout"))
  392.         nostdout = 1;
  393.     else {
  394.         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
  395.         printf ("Linux Quake -- Version %0.3f\n", LINUX_VERSION);
  396.     }
  397.  
  398.     oldtime = Sys_FloatTime () - 0.1;
  399.     while (1)
  400.     {
  401. // find time spent rendering last frame
  402.         newtime = Sys_FloatTime ();
  403.         time = newtime - oldtime;
  404.  
  405.         if (cls.state == ca_dedicated)
  406.         {   // play vcrfiles at max speed
  407.             if (time < sys_ticrate.value && (vcrFile == -1 || recording) )
  408.             {
  409.                 usleep(1);
  410.                 continue;       // not time to run a server only tic yet
  411.             }
  412.             time = sys_ticrate.value;
  413.         }
  414.  
  415.         if (time > sys_ticrate.value*2)
  416.             oldtime = newtime;
  417.         else
  418.             oldtime += time;
  419.  
  420.         Host_Frame (time);
  421.  
  422. // graphic debugging aids
  423.         if (sys_linerefresh.value)
  424.             Sys_LineRefresh ();
  425.     }
  426.  
  427. }
  428.  
  429.  
  430. /*
  431. ================
  432. Sys_MakeCodeWriteable
  433. ================
  434. */
  435. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  436. {
  437.  
  438.     int r;
  439.     unsigned long addr;
  440.     int psize = getpagesize();
  441.  
  442.     addr = (startaddr & ~(psize-1)) - psize;
  443.  
  444. //    fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
  445. //            addr, startaddr+length, length);
  446.  
  447.     r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
  448.  
  449.     if (r < 0)
  450.             Sys_Error("Protection change failed\n");
  451.  
  452. }
  453.  
  454.